const { Long } = require('bson');
const { executeQuery } = require('./mongo_util')
const collectionName = "order";



//添加订单
function addorder(order, onRetGot, onErrGot) {
    let res = parseInt(Math.random() * 10000);
    let data = new Date();
    let time = data.getTime();
    let orderid=time+''+res
    let obj = {};
    obj = order.data;
    obj.address = order.address;
    obj.orderid = orderid;
    obj.state = order.state;
    executeQuery(collectionName, (collection, callback) => {
        collection.insertOne({...obj}).then((res) => {
            console.log("res=",res);
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}


//修改订单
function updateorder(order, onRetGot, onErrGot) {
    let orderid = order.orderid;
    let data = order.data;

    executeQuery(collectionName, (collection, callback) => {
        collection.update({"orderid":orderid},{$set:{...data}}).then((res) => {
            console.log("res=",res);
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}


//删除订单
function deleteorder(order, onRetGot, onErrGot) {
    console.log(order);
    executeQuery(collectionName, (collection, callback) => {
        collection.deleteOne({"orderid":order}).then((res) => {
            console.log("res=",res);
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}

//获取所有订单
function selectall(carts,onRetGot, onErrGot) {
    let limit = carts.size*1;
    let skip = (carts.page - 1) * limit;
    console.log(limit,skip);
    executeQuery(collectionName, (collection, callback) => {
        collection.find({}).count().then(async(res) => {
            let obj = {};
            obj.total = res;

            await collection.find({}).skip(skip).limit(limit).toArray().then((bbq => {
                obj.data = bbq;
            }))
            onRetGot(obj);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}

//根据状态查询数据
function selectstate(carts,onRetGot, onErrGot) {
    let limit = carts.size*1;
    let skip = (carts.page - 1) * limit;
    let state = carts.state;
    console.log(limit,skip);
    executeQuery(collectionName, (collection, callback) => {
        collection.find({"state":state}).count().then(async(res) => {
            let obj = {};
            obj.total = res;

            await collection.find({"state":state}).skip(skip).limit(limit).toArray().then((bbq => {
                obj.data = bbq;
            }))
            onRetGot(obj);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}




function selectorder(carts,onRetGot, onErrGot) {
 
    executeQuery(collectionName, (collection, callback) => {
        collection.findOne({"orderid":carts}).then((res) => {
            console.log(res);
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}

module.exports = {
    addorder,
    updateorder,
    deleteorder,
    selectorder,
    selectall,
    selectstate
}